app.controller("productCtr",function($scope,$http,$rootScope){

	// 写控制的逻辑代码
	// 进行网络请求
	$http({
		method:"get",
		url:"php/getProducts.php"
	}).then(function(response){
		// 保存数据
		$scope.products = response.data;



	// 第一步该确定：分为多少页
	// $scope.categoryPages = Math.ceil($scope.categoryProducts.length / 3);
	$scope.pagesOfCategory = function(){

		var num = $scope.categoryPages = $scope.categoryProducts.length / 3;
		var pages = [];

		for (var i = 0; i < num; i++) {
			pages.push(i + 1);
		}

		$scope.pagesArray = pages;
	}

	// 实现分页按钮点击的函数，点击第几页就显示当前分类下面的第几页
	
	$scope.contentsPerPage = function(page){

		// 假设一页就显示3个元素
		var productsOfPage = [];
  
		// 从categoryProducts取元素  page 表示当前第几页
		for (var i = (page - 1) * 3; i < Math.min(page * 3,$scope.categoryProducts.length); i++) {
			productsOfPage.push($scope.categoryProducts[i]);
		}

		$scope.productsOfPage = productsOfPage;

		// 记录当前选中的页码
		$scope.cuurrentPage = page;

	}

		

		// 实现点击左侧分类按钮的时候，显示对于的商品
	$scope.findCategoryProducts = function(category){
	// 记录当前点击的分类
	$scope.currentCategory = category;
	var categoryProducts = [];
	if (category == "全部") {
		// 返回所有的元素
		categoryProducts = $scope.products;
	}else{
		for (var i = 0; i < $scope.products.length; i++) {
			if (category == $scope.products[i].category) {
				categoryProducts.push($scope.products[i]);
			}
		}
	}

	$scope.categoryProducts = categoryProducts;

	// 调用分页
	$scope.pagesOfCategory();
	// 调用第一页的数据
	$scope.contentsPerPage(1);
	}
	
	$scope.findCategoryProducts("全部");

	// 返回分类的class名字
	$scope.classOfCategory = function(category){
		return $scope.currentCategory == category ? "btn-primary":"";
	}


	// 当前页码图标的显示
	$scope.classOfPage = function(page){
		return $scope.cuurrentPage == page ? "btn-primary" :"";
	}



	


	




	}, function(error){
		console.log(error);
	});


})